package dca;

import java.io.IOException;
import java.util.ArrayList;
import java.util.NoSuchElementException;

public class Doctor {
	

	private DiseaseRegistry DiaseaseBase;
	
	/**
	 * A constructor that allows the user to specify which data the doctor is working with.
	 * @throws IOException 
	 */
	public Doctor(String DiseaseFile) throws IOException
	{
		this.DiaseaseBase = new DiseaseRegistry(DiseaseFile);
	}
	public Doctor() throws IOException{
		this("diseaselist090112"); //The default diseaselist is used when nothing else is passed to the constructor.
	}
	
	
	/**
	 * Diagnoses a patient and returns a cure for his/her symptoms.
	 * @param aPatient
	 * @return
	 */
	public Disease diagnose(Patient aPatient) {
		ArrayList<Disease> possibleDiseases = new ArrayList<Disease>();
		if((!(aPatient.getSymptoms()==null)) || aPatient.getSymptoms().size()<1)
		{	
			for(Disease d:this.DiaseaseBase.DiseaseList())
			{
				boolean diseaseDropped=false;
				for(Symptom s: aPatient.getSymptoms())
				{
					if(!d.getSymptoms().contains(s))
						diseaseDropped=true;
				}
				if(!diseaseDropped)
					possibleDiseases.add(d);
			}
			if(possibleDiseases.size()==1)
				return possibleDiseases.get(0);
			else if(possibleDiseases.size()>1)
			{
				Disease diagnosedDisease = possibleDiseases.get(0);
				int closestDiagnoseUnmatched = Integer.MAX_VALUE;
				for(Disease d:possibleDiseases)
				{
					int unMatchedCount= d.getSymptoms().size();
					for(@SuppressWarnings("unused") Symptom s:aPatient.getSymptoms())  //The s is only used to subtract one for each matched symptom.
					{
						unMatchedCount--;
					}
					if (unMatchedCount<closestDiagnoseUnmatched){
						closestDiagnoseUnmatched=unMatchedCount;
						diagnosedDisease=d;}
				}
				
				return diagnosedDisease;
			}
		}
		throw new NoSuchElementException("No diagnose found for patient "+ aPatient.getName());
		
		
			
		
		
		
	}

}
